import { observable, action } from 'mobx';
import * as Apis from './../apis/Api'
export class ApiStore {
    @observable apiId = "";//接口id
    @observable baseList;//基本信息
    @observable tableList;//列表
    @observable showTemplet;//显示模板
    @observable isSuccess = false;//是否成功数据
    @observable urlQuery;//拼接完后的参数
    @observable groups = [];//菜单分组
    @observable parsms = [];//改接口设置的参数
    @observable singRequestParam = {};//单接口页面接收到的参数
    @observable subTableList = [];//子列表
    @observable apiName = "";//接口名称
    @observable front_path = "";//前端路径
    @observable apiParamsList = [];//真正属于该接口的参数及参数值
    @observable updata_date = "";//数据更新时间
    @observable msgcode = "";//返回信息
    @observable loading = true;//加载状态
    @observable isList = "0";//接口内的数据为纯列表
    @observable thisPage = 1;//纯列表类接口默认显示第一页，启信宝显示前二十条
    @observable total = 1;//纯列表类接口一共有多少页
    @observable module = "";
    //处理参数
    @action
    initSingApiPage = (props) => {
        if (props.location) {//通过链接
            this.front_path = "/" + props.params.frontPath;
            this.singRequestParam = props.location.query;
        } else {//通过组件
            this.singRequestParam = props.requestParam;
            this.front_path = "/" + props.frontPath;
        }
        if (this.front_path != "/undefined") {
            this.getIdByFrontPath(this.front_path);
        }

    }
    @action
    getIdByFrontPath = (frontPath) => {
        Apis.getIdByFrontPath({ frontPath: frontPath }).then(data => {
            this.isList = data.islist;
            if (data.status == "success") {
                this.apiId = data.apiId;
                //拼接参数
                let urlQuery = "&";
                let urlQueryBeforeEncode = "";
                this.parsms = data.params;
                this.parsms.map(param => {
                    let value = this.singRequestParam[param.front_name];
                    if (value) {
                        urlQueryBeforeEncode += param.server_name + "=" + value + "&";
                        this.apiParamsList.push({ ...param, value: value });
                        if ((/.*[\u4e00-\u9fa5]+.*$/.test(value))) {
                            value = encodeURI(value);
                        }
                        urlQuery += param.server_name + "=" + value + "&";
                    }
                });

                if (this.isList == "1") {
                    urlQuery += "skip=" + (this.thisPage - 1) * 20;
                    urlQueryBeforeEncode += "skip=" + (this.thisPage - 1) * 20;
                }
                this.apiName = data.apiName;
                this.front_path = data.front_path;
                this.showTemplet = data.showTemplet;
                this.callApi(this.apiId, urlQuery, urlQueryBeforeEncode);
            } else {
                this.msgcode = data.msgcode;
                this.isSuccess = false;
                this.loading = false;
            }
        })
    }
    //调用API
    @action
    callApi = (id, urlQuery, urlQueryBeforeEncode) => {
        Apis.getApi({ apiId: id, query: urlQuery, urlQueryBeforeEncode: urlQueryBeforeEncode, isList: this.isList }).then(data => {
            if (data.status == "success") {
                if (data.datas.baseList) {
                    this.baseList = data.datas.baseList;
                }
                this.tableList = data.datas.tableList;
                this.subTableList = data.datas.sublist;
                this.updata_date = data.updata_date + " " + data.updata_time;
                this.total = Math.ceil(parseInt(data.total) / 20);
                this.isSuccess = true;
            } else {
                this.msgcode = data.msgcode;
                this.isSuccess = false;
            }
            this.loading = false;
        })
    }

    //多接口页面获取菜单
    @action
    getApiMenu = (props) => {
        if (props.location) {//通过链接
            this.requestParam = props.location.query;
            this.module = props.params.module;
        } else {//通过组件
            this.module = props.module;
            this.requestParam = props.requestPara;
        }

        Apis.getMenu({ module: this.module }).then(data => {
            if (data.status == "success") {
                this.groups = data.data;
                this.getIdByFrontPath(data.default_path);
            }
        })
    }
    //多接口页面切换菜单时
    @action
    changeMenu = (key) => {
        this.clrarCatch();
        this.thisPage = 1;
        this.total = 1;
        this.getIdByFrontPath(key);
    }
    @action
    getSubTable = (subTable, columnName) => {
        this.subTable = subTable;
        this.columnName = columnName;
        this.visible = true;
    }
    //页码+1
    @action
    countPage = () => {
        if (this.total > this.thisPage) {
            this.clrarCatch();
            this.thisPage++;
            this.getIdByFrontPath(this.front_path);
        }
    }
    //页码-1
    @action
    cutPage = () => {
        if (this.thisPage > 1) {
            this.clrarCatch();
            this.thisPage--;
            this.getIdByFrontPath(this.front_path);
        }
    }
    @action
    clrarCatch = () => {
        this.apiParamsList = [];
        this.msgcode = "";
        this.isList = "0";
        this.updata_date = "";
        this.isSuccess = false;
        this.loading = true;
    }
}